#include "common.h"
#include "shadow.h"


#define RAY_PATH	2.0f

#define	JITTER_SUN_SHAFTS

#ifdef	SUN_SHAFTS_QUALITY
	#if SUN_SHAFTS_QUALITY==1
		#define	FILTER_LOW
		#define RAY_SAMPLES	20
	#elif SUN_SHAFTS_QUALITY==2
		#define RAY_SAMPLES	20
	#elif SUN_SHAFTS_QUALITY==3
		#define RAY_SAMPLES	40
	#endif
#endif

uniform half4	volume_range;	//	x - near plane, y - far plane
uniform half4	sun_shafts_intensity;

layout(location = TEXCOORD0) in vec2	tc;
layout(location = TEXCOORD1) in vec2	tcJ;

layout(location = COLOR0) out vec4	oColor;

void 	main ()
{
#ifndef	SUN_SHAFTS_QUALITY
	oColor = half4(0,0,0,0);
#else	//	SUN_SHAFTS_QUALITY

	half3	P = tex2D(s_position, tc).xyz;

#ifndef	JITTER_SUN_SHAFTS
//	Fixed ray length, fixed step dencity
//	half3	direction = (RAY_PATH/RAY_SAMPLES)*normalize(P);	
//	Variable ray length, variable step dencity
	half3	direction = P/RAY_SAMPLES;
#else	//	JITTER_SUN_SHAFTS
//	Variable ray length, variable step dencity, use jittering
	half4	J0 	= tex2D		(jitter0,tcJ);
	half	coeff = (RAY_SAMPLES - 2*J0.x)/(RAY_SAMPLES*RAY_SAMPLES);		
	half3	direction = P*coeff;
//	half3	direction = P/(RAY_SAMPLES+(J0.x*4-2));
#endif	//	JITTER_SUN_SHAFTS

	half	depth = P.z;
	half	deltaDepth = direction.z;
	
	half4	current	= mul (m_shadow,float4(P,1));
	half4	delta 	= mul (m_shadow, float4(direction,0));

	half	res = 0;
	half	max_density = sun_shafts_intensity.x;
	half	density = max_density/RAY_SAMPLES;

	if (depth<0.0001)
		res = max_density;

	for ( int i=0; i<RAY_SAMPLES; ++i )
	{
		if (depth>0.3)
		{
#ifndef	FILTER_LOW
				res += density*shadow(current);
#else	//	FILTER_LOW
				res += density*sample_hw_pcf(current, float4(0,0,0,0));
#endif	//	FILTER_LOW
		}

		depth -= deltaDepth;
		current -= delta;
	}

	float fSturation = -Ldynamic_dir.z;

	//	Normalize dot product to
	fSturation	= 0.5*fSturation+0.5;
	//	Map saturation to 0.2..1
	fSturation	= 0.80*fSturation+0.20;

	res		*= fSturation;

	oColor	= res*Ldynamic_color;
#endif	//	SUN_SHAFTS_QUALITY
}
